package com.wing.productionPlan.controller.admin;



import cn.hutool.core.bean.BeanUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.enumeration.AuditStatusEnum;
import com.wing.productionPlan.model.entity.DayTakeGoodsIterm;
import com.wing.productionPlan.model.entity.RequirePlanItem;
import com.wing.productionPlan.model.response.DayTakeGoodsItermVO;
import com.wing.productionPlan.model.response.DayTakeGoodsVO;
import com.wing.productionPlan.model.response.RequirePlanItermVO;
import com.wing.productionPlan.model.response.RequirePlanVO;
import com.wing.productionPlan.service.DayTakeGoodsItermService;
import com.wing.productionPlan.service.DayTakeGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Validated
@Api(value = "Admin-生产管理-日提货订单详情", tags = "#/product/requirePlan/dayTakeGoodsItermList")
@RestController("DayTakeGoodsItermController")
@RequestMapping("productionPlan/admin/dayTakeGoodsIterm")
@RequiredArgsConstructor
public class DayTakeGoodsItermController {


    private final  DayTakeGoodsItermService dayTakeGoodsItermService;

    private final DayTakeGoodsService dayTakeGoodsService;

    @ApiOperation(value = "查询日提货明细列表", notes = "搜索列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<DayTakeGoodsItermVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String takeOrderNumber
            ,String factoryName
            ,String startCreateTime
            ,String endCreateTime
    ) {
        Page<DayTakeGoodsItermVO> pg = new Page<>(page, limit);
        HashMap map = new HashMap();
        map.put("takeOrderNumber", takeOrderNumber);
        map.put("factoryName", factoryName);
        if (StringUtils.isNotBlank(startCreateTime)) {
            map.put("startCreateTime", startCreateTime);
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            map.put("endCreateTime", endCreateTime);
        }
        Page<DayTakeGoodsItermVO> list = dayTakeGoodsItermService.pageList4app(pg, map);
        List<DayTakeGoodsItermVO> records = list.getRecords();

        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "查询日提货明细列表详情", notes = "搜索")
    @GetMapping("/getTakeGoodsItermDetail")
    public JsonResult getPlanItermDetail(@RequestParam("id") Long id, @RequestParam("takeGoodsId") Long takeGoodsId) {
        DayTakeGoodsVO appById = dayTakeGoodsService.get4appById(takeGoodsId);
        if(appById!=null){
           /* appById.setAuditStatusText(AuditStatusEnum.getDescriptionByCode(appById.getAuditStatus()));*/
            DayTakeGoodsItermVO planItermById = dayTakeGoodsItermService.getTakeGoodsItermById(id);
            List<DayTakeGoodsIterm>  requirePlanItemList=new ArrayList<>();
            DayTakeGoodsIterm requirePlanItem=new DayTakeGoodsIterm();
            BeanUtil.copyProperties(planItermById, requirePlanItem,true);
            requirePlanItemList.add(requirePlanItem);
            appById.setDayTakeGoodsItermList(requirePlanItemList);
            return JsonResult.success(appById);
        }else{
            return JsonResult.fail("未发现详情数据！");
        }
    }




}
